/******************************************************************************
 *
 * Copyright:
 *    (C) 2000 - 2005 Embedded Artists AB
 *
 *****************************************************************************/


#include "../pre_emptive_os/api/osapi.h"
#include "../pre_emptive_os/api/general.h"
#include <printf_P.h>
#include <lpc2xxx.h>
#include "../startup/config.h"
#include "testAdc.h"
#include "testMotor.h"

/*****************************************************************************
 *
 * Description:
 *    initialize step motor.
 *
 ****************************************************************************/

void initMotor() {
	//set pins P0.12 and P0.21 on output mode
	IODIR0 |= 0x00201000;

	initPot();
}

/*****************************************************************************
 *
 * Description:
 *   Method turns motor clockwise or anticlokwise
 *
 * Returns current step motor index
 *
 ****************************************************************************/
tS8 stepMotor(tS8 stepmotorIndex, tS8 direction,  tU32 power) {
	//						  0 degrees, 90 degrees, 180 degrees, 270 degrees
	tU32 stepmotorSteps[4] = {0x00201000, 0x00200000, 0x00000000, 0x00001000};

	tS8 calculatedStepIndex = calculateMotorStepIndex(stepmotorIndex, direction);

	//set both pins on "low" state
	IOCLR0 = 0x00201000;
	//set motor step using pins P0.12 and P0.21
	IOSET0 = stepmotorSteps[calculatedStepIndex];
	osSleep(power/20 + 50);

	return calculatedStepIndex;
}
/*****************************************************************************
 *
 * Description:
 *    Method calculates and returns current index of motor step
 *
 ****************************************************************************/
tS8 calculateMotorStepIndex(tS8 stepmotorIndex, tS8 direction) {
	tS8 motorIndexLocal = stepmotorIndex;
	if (direction == -1) {
		motorIndexLocal --;
		if (motorIndexLocal < 0) {
			motorIndexLocal = 3;
		}
	} else {
		motorIndexLocal ++;
		if (motorIndexLocal > 3) {
			motorIndexLocal = 0;
		}
	}

	return motorIndexLocal;
}
/*****************************************************************************
 *
 * Description:
 *    Initialize potentiometer
 *
 ****************************************************************************/
void initPot() {
	//reset P0.28 (potentiometer)
	PINSEL1 &= ~0x3000000; //25 and 26 bit are set to 0, other bits equals 1
	//set AN1 function for P0.28
	PINSEL1 |= 0x1000000; //25 bit is set to 0, 26 bit is set to 1. Other bits equals 0

	//initialize ADC - PAGE 288 of users' guide
	ADCR = (1 << 0) |  //SEL = 1, dummy channel #1
			((CRYSTAL_FREQUENCY * PLL_FACTOR / VPBDIV_FACTOR) / 4500000 - 1) << 8 |  //set clock division factor, so ADC clock is 4.5MHz
			(0 << 16) |  //BURST = 0, conversions are SW controlled
			(1 << 17) |  //CLKS  = 0, 10-bit result
			(1 << 21) |  //PDN   = 1, ADC is active
			(1 << 24) |  //START = 1, start a conversion now
			(0 << 27);	 //EDGE  = 0, irrelevant when START = 1
}
/*****************************************************************************
 *
 * Description:
 *    Read potentiometer value
 *
 ****************************************************************************/
tU32 readPotValue() {
	tU32 integerResult;

	//short delay and dummy read
	osSleep(1);
	integerResult = ADDR;

	//get AIN1 (P0.28)
	integerResult = getAnalogueInput(AIN1);


	return integerResult;
}
